/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vs.kindlecoll.UI;

import com.vs.kindlecoll.Beans.Book;
import com.vs.kindlecoll.Beans.BookObject;
import com.vs.kindlecoll.Beans.BooksCollection;
import com.vs.kindlecoll.IO.KindleInteractor;
import java.awt.Component;
import java.awt.Container;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author volodymyr.sadovyy
 */
public class UIHelper {

    public CollectionTreeModel getTreeModel() {
        List<BooksCollection> colls = new LinkedList<BooksCollection>();
        try {
            colls = KindleInteractor.getCollections();
        } catch (Exception ex) {
            Logger.getLogger(KindleCollView.class.getName()).log(Level.SEVERE, null, ex);
        }

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("My Kindle");
        for (BooksCollection c : colls) {
            DefaultMutableTreeNode collection = new DefaultMutableTreeNode(c);
            collection.setAllowsChildren(true);

            for (Book b : c.getBooks()) {
                DefaultMutableTreeNode book = new DefaultMutableTreeNode(
                        new Book(b));
                collection.add(book);
            }
            root.add(collection);
        }
        CollectionTreeModel model = new CollectionTreeModel(root);

        return model;

    }

    public void updateTreeModel(JTree tree) {
        CollectionTreeModel c = (CollectionTreeModel) tree.getModel();
        c = null;
        tree.setModel(new UIHelper().getTreeModel());
    }

    public void showBookProperties(Component c) {
        BookObject[] bo = null;
        Container topLevel = null;
        if (c.getClass().equals(JTree.class)) {
            JTree tree = (JTree) c;
            tree.getSelectionPath();
            topLevel = tree.getTopLevelAncestor();
            bo = (BookObject[]) getSelectedObjects(tree);
        } else if (c.getClass().equals(JList.class)) {
            JList<Book> list = (JList<Book>) c;
            bo = list.getSelectedValuesList().toArray(new BookObject[list.getSelectedIndices().length]);
        }

        if (null != bo) {
            StringBuilder sb = new StringBuilder();
            for (BookObject o : bo) {
                sb.append(o.getPropertiesString()).append("\n");
            }

            JOptionPane.showMessageDialog(topLevel, "Properties:\n" + sb.toString());
        }

    }

    public BookObject[] getSelectedObjects(JTree tree) {
        TreePath[] paths = tree.getSelectionPaths();

        if (null != paths) {
            BookObject[] objects = new BookObject[paths.length];
            for (int i = 0; i < paths.length; i++) {
                DefaultMutableTreeNode node = ((DefaultMutableTreeNode) paths[i].getLastPathComponent());

                if (node.getUserObject() instanceof BookObject) {
                    BookObject o = (BookObject) node.getUserObject();

                    if (o.getClass().equals(Book.class)
                            || o.getClass().equals(BooksCollection.class)) {

                        objects[i] = o;
                    }
                }else{
                    return null;
                }
            }
            return objects;
        }
        return null;
    }

    public BooksCollection getSelectedParentObject(JTree tree) {
        TreePath path = tree.getSelectionPath();
        if (null != path) {
            DefaultMutableTreeNode selected = ((DefaultMutableTreeNode) path.getLastPathComponent());
            DefaultMutableTreeNode o = (DefaultMutableTreeNode) selected.getParent();

            //Parent can be only collection
            if (o.getUserObject().getClass().equals(BooksCollection.class)) {
                return (BooksCollection) o.getUserObject();
            }
        }
        return null;
    }

    public void delCollectionElement(JTree jTree) {
        BookObject[] objects = getSelectedObjects(jTree);

        if (null != objects) {
            for (BookObject o : objects) {
                if (o.getClass().equals(Book.class)) {
                    BookObject po = getSelectedParentObject(jTree);
                    try {
                        KindleInteractor.delCollsBook((Book) o, (BooksCollection) po);
                    } catch (Exception ex) {
                        Logger.getLogger(UIHelper.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (o.getClass().equals(BooksCollection.class)) {
                    KindleInteractor.delCollection((BooksCollection) o);
                } else {
                    Logger.getLogger(PopupMenu.class.getName()).log(Level.SEVERE, "Unknown object to delete");
                }
            }
            updateTreeModel(jTree);
        }
    }

    public void delBookElement(JList<Book> list) {
        if (list.getModel().getSize() > 0) {
            List<Book> booksToDelete = list.getSelectedValuesList();
            if (null != booksToDelete && !booksToDelete.isEmpty()) {
                KindleInteractor.delListBook(booksToDelete);
                list.repaint();
            }
        }
    }
}
